﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        #region fonctions class
        ////////////////////////FONCTIONS CLASS////////////////////////////////


        //Listing des classes enregistrées
        private void list_classes_offline(ListBox maListe)
        {
            maListe.Items.Clear();

            int id = (from i in db_local.Campus
                      where i.Name.Equals(campus_listbox2.SelectedValue)
                      select i.Id).FirstOrDefault();

            foreach (Classes s in (from e in db_local.Classes where e.Campus_id == id select e))
            {
                maListe.Items.Add(s.Name);

            }
        }

        //Listing des cursus enregistrés
        private void list_cursus_offline()
        {
            class_cursus_combobox.Items.Clear();

            foreach (Cursus s in (from e in db_local.Cursus select e))
            {
                class_cursus_combobox.Items.Add(s.Name);

            }
        }


        //Affichage de la liste des classes
        private void display_classes_offline(String classes)
        {
            foreach (Classes s in (from e in db_local.Classes select e))
            {
                if (s.Name == classes)
                {
                    class_name_textbox.Text = s.Name;
                    int cursus_id_temp = (int)s.Cursus_id;

                    string cursus_name = (from i in db_local.Cursus
                                          where i.Id.Equals(cursus_id_temp)
                                          select i.Name).FirstOrDefault().ToString();

                    class_cursus_combobox.Items.Clear();
                    class_cursus_combobox.Items.Add(cursus_name);
                    class_cursus_combobox.SelectedValue = cursus_name;
                }

            }
        }


        //Rafraichissement des éléments
        private void refresh_classes_offline()
        {
            class_name_textbox.Text = "";
            list_classes_offline(class_listbox);
            list_cursus_offline();
            list_campus_offline(campus_listbox2);
        }


        //Ajout d'une classe
        private void add_classes_offline()
        {
            Classes class1 = new Classes();


            class1.Campus_id = (from i in db_local.Campus
                                where i.Name.Equals(campus_listbox2.SelectedValue)
                                select i.Id).FirstOrDefault();

            class1.Name = class_name_textbox.Text.ToLower();

            int cursus_id = (from i in db_local.Cursus
                             where i.Name.Equals(class_cursus_combobox.SelectedValue.ToString())
                             select i.Id).FirstOrDefault();

            class1.Cursus_id = cursus_id;

            db_local.Classes.InsertOnSubmit(class1);
            db_local.SubmitChanges();

            //Rafraichissement des éléments
            refresh_classes_offline();


        }


        //Mise à jour d'une classe
        private void update_classes_offline()
        {
            if (class_name_textbox.Text != "")
            {
                Classes class2 = (from e in db_local.Classes
                                  where e.Name == (string)class_listbox.SelectedValue
                                  select e).FirstOrDefault();

                class2.Name = class_name_textbox.Text.ToLower();

                int cursus_id = (from i in db_local.Cursus
                                 where i.Name.Equals(class_cursus_combobox.SelectedValue.ToString())
                                 select i.Id).FirstOrDefault();

                class2.Cursus_id = cursus_id;

                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_classes_offline();
            }

        }


        //Suppression d'une classe
        private void delete_classes_offline()
        {
            if (class_name_textbox.Text != "")
            {
                Classes class3 = (from e in db_local.Classes
                                  where e.Name == (string)class_listbox.SelectedValue
                                  select e).FirstOrDefault();

                db_local.Classes.DeleteOnSubmit(class3);
                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_classes_offline();
            }

        }

        private void student_campus_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            chargement_liste_classe_student_offline();
        }


        #endregion
    }
}
